<?php

/**
 * Customer short summary.
 *
 * Customer description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Customer extends Shop_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Customer_model');
        //Region_model
        $this->load->Model('Region_model');
        $this->load->Model('Customer_region_model');
        $this->load->Model('Goods_order_model');
        $this->load->library('form_validation');

    }
    public function r_login()
    {
        $data['backurl']=$this->input->get('backurl');
        $this->load->view('Pre/login/r_login.html',$data);
    }
    public function index()
    {

        $bacrurl='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

        $this->check_redirect($bacrurl);
        $customerModel=$this->Customer_model;
        $customerid=$this->get_customer_id();
        $data['customerModel'] =$customerModel->get_customer_byid($customerid);
        $data['customer_add_list']=$this->Customer_region_model->get_customer_region_by_customer_id($customerid);
        $data['customer_order_list']=$this->Goods_order_model->get_order_by_customer_id($customerid);
        // p($data['customer_order_list']);
        $this->load->view('Pre/customer/index.html',$data);
    }

    public function update_d()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $valiRes= $this->form_validation->run('account_u_d_vali');

            if($valiRes)
            {
                $this->Customer_model->account=$this->input->post('account');
                $this->Customer_model->phone=$this->input->post('phone');
                $this->Customer_model->email=$this->input->post('email');
                $this->Customer_model->customer_id=$this->input->post('customer_id');
                $customerModel=$this->Customer_model;
                $res= $this->Customer_model->update_customer_d($customerModel);
                if($res)
                {
                    $this->load->library('session');
                    $_customerModel=$this->Customer_model->get_customer_byid($customerModel->customer_id);
                    $this->session->set_userdata(get_object_vars($_customerModel));
                    $this->ajaxmsgmodel->Msg='修改成功';
                    $this->ajaxmsgmodel->Statu='ok';
                    $this->ajaxmsgmodel->BackUrl=site_url('customer/index');

                }
                else
                {
                    $this->ajaxmsgmodel->Msg='修改失败';
                    $this->ajaxmsgmodel->Statu='err';
                }
            }
            else
            {
                $errors=  $this->form_validation->error_array();
                $this->ajaxmsgmodel->Msg='验证失败:'.get_validate_errors($errors);;
                $this->ajaxmsgmodel->Statu='err';
            }
        }else{
            $this->ajaxmsgmodel->Msg='未登录';
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('backurl');
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function update_p()
    {
        //$this->check_redirect();
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $valiRes= $this->form_validation->run('account_u_p_vali');

            if($valiRes)
            {
                $this->Customer_model->pass=md5($this->input->post('pass'));
                $this->Customer_model->customer_id=$this->input->post('customer_id');
                $customerModel=$this->Customer_model;
                $res= $this->Customer_model->update_customer_p($customerModel);
                if($res)
                {
                    $this->load->library('session');
                    $_customerModel=$this->Customer_model->get_customer_byid($customerModel->customer_id);
                    $this->session->set_userdata(get_object_vars($_customerModel));
                    $this->ajaxmsgmodel->Msg='修改成功';
                    $this->ajaxmsgmodel->Statu='ok';
                    $this->ajaxmsgmodel->BackUrl=site_url('customer/index');

                }
                else
                {
                    $this->ajaxmsgmodel->Msg='修改失败';
                    $this->ajaxmsgmodel->Statu='err';
                }
            }
            else
            {
                $errors=  $this->form_validation->error_array();
                $this->ajaxmsgmodel->Msg='验证失败:'.get_validate_errors($errors);;
                $this->ajaxmsgmodel->Statu='err';
            }
        }else{
            $this->ajaxmsgmodel->Msg='未登录';
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('p_url');
        }

        echo(json_encode($this->ajaxmsgmodel));
    }

    public function check_unique()
    {
        $val=$this->input->get('account');
        $mark=$this->input->get('mark');
        $cid=$this->input->get('cid');
        $res='true';

        if($mark=='1'){
            $ures=$this->Customer_model->CheckAddAccountName($val);
            if($ures>1){
                $res='false';
            }else{
                $res='true';
            }
        }else{
            $ures=$this->Customer_model->CheckEditAccountName($val,$cid);
            if($ures>1){
                $res='false';
            }else{
                $res='true';
            }
        }
        echo $res;
    }

    //添加地址页面加载
    public function add_region()
    {
        $this->frame_check_redirect();
        $data['customer_id']=$this->uri->segment(3);
        $data['goods_order_id']=$this->uri->segment(4);
        $data['provincelist']=$this->Region_model->get_province();
        //p($data['provincelist']);
        $this->load->view('Pre/customer/add_region.html',$data);
    }

    public function get_region_id()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $region_id=$this->input->get('region_id');
            $regionlist=$this->Region_model->get_region_id($region_id);

            if(!empty($regionlist) &&  count($regionlist)>0)
            {
                $this->ajaxmsgmodel->Data=$regionlist;
                $this->ajaxmsgmodel->Statu='ok';
            }
            else
            {
                $this->ajaxmsgmodel->Statu='err';
            }
        }else{
            $this->ajaxmsgmodel->Msg='未登录';
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->get('backurl');
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    /**
     * Summary of 添加客户邮寄地址
     */
    public function add_customer_add()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $valiRes= $this->form_validation->run('customer_add_vali');

            if($valiRes)
            {
                $this->Customer_region_model->c_name=$this->input->post('c_name');
                $this->Customer_region_model->phone=$this->input->post('phone');
                $this->Customer_region_model->tel=$this->input->post('tel');
                $this->Customer_region_model->province=$this->input->post('province');
                $this->Customer_region_model->city=$this->input->post('city');
                $this->Customer_region_model->county=$this->input->post('county');
                $this->Customer_region_model->detailed_add=$this->input->post('detailed_add');
                $this->Customer_region_model->customer_id=$this->input->post('customer_id');
                //判断该用户是否有地址列表，如无，设置默认，如有不设置
                $customer_region=$this->Customer_region_model->get_customer_region_by_customer_id($this->Customer_region_model->customer_id);

                if(empty($customer_region)){
                    $this->Customer_region_model->is_default=1;
                }else{
                    $this->Customer_region_model->is_default=0;
                }

                $customer_regionModel=$this->Customer_region_model;
                $res= $this->Customer_region_model->inset_customer_region_from_order($customer_regionModel,$this->input->post('goods_order_id'));
                if($res)
                {
                    $this->ajaxmsgmodel->Msg='添加成功';
                    $this->ajaxmsgmodel->Statu='ok';
                    $this->ajaxmsgmodel->BackUrl=$this->input->post('p_url');//返回上级页面

                }
                else
                {
                    $this->ajaxmsgmodel->Msg='添加失败';
                    $this->ajaxmsgmodel->Statu='err';
                }
            }
            else
            {
                $errors=  $this->form_validation->error_array();
                $this->ajaxmsgmodel->Msg='验证失败:'.get_validate_errors($errors);;
                $this->ajaxmsgmodel->Statu='err';
            }
        }else{
            $this->ajaxmsgmodel->Msg='未登录';
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('backurl');
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
    //设置默认地址
    public function set_default()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {

            $customer_regionModel=new Customer_region_model();
            $customer_region_id=$this->input->post("cr_id");
            $res= $customer_regionModel->set_default($customer_region_id);
            if($res==1)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$this->input->post('backurl');
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $this->ajaxmsgmodel->Msg='未登录';
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('backurl');;
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
    //修改地址页面加载
    public function update_region()
    {
        $this->frame_check_redirect();
        $customer_region_id=$this->input->get('cr_id');
        $data['customer_regionmodel']=$this->Customer_region_model->get_region_by_id($customer_region_id);
        $data['provincelist']=$this->Region_model->get_province();
        $this->load->view('Pre/customer/update_region.html',$data);
    }

    public function update_customer_add()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $valiRes= $this->form_validation->run('customer_add_vali');

            if($valiRes)
            {
                $this->Customer_region_model->customer_region_id=$this->input->post('customer_region_id');
                $this->Customer_region_model->c_name=$this->input->post('c_name');
                $this->Customer_region_model->phone=$this->input->post('phone');
                $this->Customer_region_model->tel=$this->input->post('tel');
                $this->Customer_region_model->province=$this->input->post('province');
                $this->Customer_region_model->city=$this->input->post('city');
                $this->Customer_region_model->county=$this->input->post('county');
                $this->Customer_region_model->detailed_add=$this->input->post('detailed_add');
                $customer_regionModel=$this->Customer_region_model;
                $res= $this->Customer_region_model->update_customer_region($customer_regionModel);
                if($res)
                {
                    $this->ajaxmsgmodel->Msg='修改成功';
                    $this->ajaxmsgmodel->Statu='ok';
                    $this->ajaxmsgmodel->BackUrl=$this->input->post('p_url');

                }
                else
                {
                    $this->ajaxmsgmodel->Msg='修改失败';
                    $this->ajaxmsgmodel->Statu='err';
                }
            }
            else
            {
                $errors=  $this->form_validation->error_array();
                $this->ajaxmsgmodel->Msg='验证失败:'.get_validate_errors($errors);;
                $this->ajaxmsgmodel->Statu='err';
            }
        }else{
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->Msg='未登录';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('backurl');
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function delete_region()
    {
        $this->load->library('ajaxMsgModel');
        if($this->check())
        {
            $region_id=$this->input->get('cr_id');
            $backurl=$this->input->post('back_url');
            $res= $this->Customer_region_model->delete_customer_region($region_id);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$backurl;

            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }


        }else{
            $this->ajaxmsgmodel->Statu='okto';
            $this->ajaxmsgmodel->Msg='未登录';
            $this->ajaxmsgmodel->BackUrl=site_url('customer/r_login').'?backurl='.$this->input->post('back_url');
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}